package com.elong.android.specialhouse.ui;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.elong.android.specialhouse.R;

public class FilterAddSubView extends FrameLayout implements View.OnClickListener{

    private FrameLayout mRootView;
    private ImageView mPlus;
    private ImageView mMinus;
    private TextView tvCount;
    private int mCount = 1;
    /**
     * 默认最小数为1,可通过setMin方法或者在布局文件中使用app:min=""设置
     */
    private int mMin = 1;
    /**
     * 默认最大数为15,可通过setMax方法或者在布局文件中使用app:max=""设置
     */
    private int mMax = 15;

    public FilterAddSubView(Context context) {
        super(context);
        init(context, null);
    }

    public FilterAddSubView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mRootView = (FrameLayout) View.inflate(context, R.layout.filter_add_sub_view, this);
        mPlus = (ImageView) mRootView.findViewById(R.id.iv_num_plus);
        mMinus = (ImageView) mRootView.findViewById(R.id.iv_num_minus);
        tvCount = (TextView) mRootView.findViewById(R.id.tv_count);
        if (attrs != null && getContext() != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.AddSubView);
            if (typedArray != null) {
                mMax = typedArray.getInt(R.styleable.AddSubView_youfang_max, 15);
                mMin = typedArray.getInt(R.styleable.AddSubView_youfang_min, 1);
                typedArray.recycle();
            }
        }
        mPlus.setOnClickListener(this);
        mMinus.setOnClickListener(this);

        // 默认加号为灰色，加号为亮色
        mPlus.setImageResource(R.drawable.icon_orange_plus);
        mMinus.setImageResource(R.drawable.icon_gray_minus);
        mMinus.setClickable(false);

        // 数量默认为1
        mCount = 1;
        tvCount.setText(mCount + "");
    }

    @Override
    public void onClick(View v) {
//        String tempCount = tvCount.getText().toString().trim();
//        if (tempCount.contains("+")) {
//            mCount = mMax + 1;
//        } else {
//            mCount = Integer.valueOf(tempCount);
//        }

        switch (v.getId()) {
            case R.id.iv_num_plus:
                // 点击加号
                if (!mMinus.isClickable()) {
                    mMinus.setClickable(true);
                    mMinus.setImageResource(R.drawable.icon_orange_minus);
                }
                mCount++;
                if (mCount > mMax) {
                	tvCount.setText(mMax + "+");
                    mPlus.setClickable(false);
                    mPlus.setImageResource(R.drawable.icon_gray_plus);
                    return;
                }
                tvCount.setText(mCount + "");
                break;
            case R.id.iv_num_minus:
                // 点击减号
                if (!mPlus.isClickable()) {
                    mPlus.setClickable(true);
                    mPlus.setImageResource(R.drawable.icon_orange_plus);
                }
                mCount--;
                if (mCount <= mMin&& mCount !=0) {
                    mMinus.setClickable(false);
                    mMinus.setImageResource(R.drawable.icon_gray_minus);
                    tvCount.setText(mCount + "");
                    return;
                }
                if(mCount <= mMin&& mCount ==0){
                    mMinus.setClickable(false);
                    mMinus.setImageResource(R.drawable.icon_gray_minus);
                    tvCount.setText(R.string.unlimited);
                    return;
                
                }
                tvCount.setText(mCount + "");
                break;

            default:
                break;
        }
    }
    
    public int getCount() {
        return mCount;
    }

    public void setCount(int Count) {
        this.mCount = Count;
        if(mCount == mMin){
        	mMinus.setClickable(false);
        	mPlus.setClickable(true);
        	mMinus.setImageResource(R.drawable.icon_gray_minus);
        	mPlus.setImageResource(R.drawable.icon_orange_plus);
        }else if(mCount > mMin && mCount <= mMax){
        	mMinus.setClickable(true);
        	mPlus.setClickable(true);
        	mMinus.setImageResource(R.drawable.icon_orange_minus);
        	mPlus.setImageResource(R.drawable.icon_orange_plus);
        }else if(mCount > mMax){
        	mMinus.setClickable(true);
        	mPlus.setClickable(false);
        	mMinus.setImageResource(R.drawable.icon_orange_minus);
        	mPlus.setImageResource(R.drawable.icon_gray_plus);
        	tvCount.setText(mMax + "+");
        	return;
        }
        if(mCount == 0){
            tvCount.setText(R.string.unlimited);
        }else{
            tvCount.setText(mCount + "");
        }

    }
    /**
     * 设置最小值
     *
     * @param Min
     */
    public void setMin(int Min) {
        this.mMin = Min;
    }


    /**
     * 设置最大值
     *
     * @param Max
     */
    public void setMax(int Max) {
        this.mMax = Max;
    }
}
